<?php
namespace app\controllers;

use app\commission\ReceivedCommission;
use app\models\order\Order;
use app\models\order\Order as Model;
use app\models\system\SetBasic;
use app\models\user\UserAccount;
use app\models\user\UserCommission;
use app\models\UserBalance;
use app\util\Debug;

class TimeoutReceivedController extends Controller
{
    /**
     * timeout received
     *
     *  进云仓的到时间会处理,
     *  发货的[提货的], 必须要是发货状态, 到时间才处理
     *
     * 30 2 * * * /usr/bin/curl https://jj2024-api.97app.cn/timeout-received/run
     *
     * 取消支付
     * 30 * * * * /usr/bin/curl https://jj2024-api.97app.cn/timeout-received/cancel
     *
     * /timeout-received/run
     */
    public function actionRun()
    {
        // 不是进云仓的订单
        // 直接发货的, 或者 提货的
        $query = Order::find();

        $orders = $query->orderBy(['id' => SORT_DESC])
            ->all();

        $set = SetBasic::getLastOne();

        $time = time();
        $expired = 3600 * 24 * $set->order_received_timeout;
        // $expired = 1;
        foreach ($orders as $order) {
            $t = $order->create_time;
            $t = strtotime($t);

            if ($time - $t >= $expired) {
                if (ReceivedCommission::done($order->id)) {

                    // 不是进云仓的订单
                    if ($order->is_store == 0) {
                        $order->status = Order::$statusMapping['RECEIVED'];
                    }

                    $order->save();
                }
            }
        }

        return "\n" . date('Y-m-d H:i:s') . ' .......  Done! ' . "\n";
    }

    // 取消订单
    public function actionCancel()
    {
        $orders = Order::find()
            ->where(['status' => Order::$statusMapping['NON_PAYMENT']])
            ->all();

        $transaction = \Yii::$app->db->beginTransaction();
        try {
            if ($orders) {
                foreach ($orders as $order) {
                    $order->status = Model::$statusMapping['DELETE'];

                    if ($order->save()) {

                        // 退回余额
                        $account = UserAccount::findOne(['user_id' => $order->user_id]);
                        if ($account && $account->lock_balance >= $order->use_balance) {

                            UserBalance::addLog($order->user_id,
                                $account->balance, +$order->use_balance, $account->balance + $order->use_balance, '取消订单', $order->id);

                            $account->lock_balance -= $order->use_balance;
                            $account->balance += $order->use_balance;

                            // 日志
                            $log = new UserCommission();
                            $log->user_id = $this->user->id;

                            // 联娱券
                            if ($account->lock_score >= $order->use_score) {
                                $account->lock_score -= $order->use_score;
                                $account->score += $order->use_score;

                                $log->score = $order->use_score;

                            }
                            // 定向券
                            if ($account->lock_ticket >= $order->use_ticket) {
                                $account->lock_ticket -= $order->use_ticket;
                                $account->ticket += $order->use_ticket;

                                $log->ticket = $order->use_ticket;
                            }

                            $log->order_id = $order->id;
                            $log->status = 1;
                            $log->award_type = '233';
                            $log->award_name = '取消订单';
                            $log->save();

                            $account->save();
                        }

                        echo $order->id, ' .... ok <br/>';

                    }
                }
            }
            $transaction->commit();
        } catch (\Exception $e) {
            $transaction->rollBack();
            Debug::commission('cancel', $e->getMessage());
        }

    }
}